//
// Created by mgr on 2019/1/3.
//

#include "CRuisMainFramWnd.h"

namespace cliMain {
    CRuisMainFramWnd::CRuisMainFramWnd():CRuisBaseWindow("main","skin.xml"){
        wnd_tab1=NULL;
    }
    CRuisMainFramWnd::~CRuisMainFramWnd(){
    }

    void CRuisMainFramWnd::Init(){
        //CWkeWebkitUI::WkeWebkit_Init();
        /*btnClose = dynamic_cast<CButtonUI*>(m_PaintManager.FindControl(_T("closebtn")));
        btnMax = dynamic_cast<CButtonUI*>(m_PaintManager.FindControl(_T("maxbtn")));
        btnRestore = dynamic_cast<CButtonUI*>(m_PaintManager.FindControl(_T("restorebtn")));
        btnMin = dynamic_cast<CButtonUI*>(m_PaintManager.FindControl(_T("minbtn")));
        pTabLayout = dynamic_cast<CTabLayoutUI*>(m_PaintManager.FindControl(_T("switch")));
        pTabLayout_tab0 = dynamic_cast<CHorizontalLayoutUI*>(m_PaintManager.FindControl(_T("switch_tab0")));*/
//        pHomeWeb = dynamic_cast<ydtWebBrowserUI*>(m_PaintManager.FindControl(_T("webui")));
        //pWebkit = dynamic_cast<CWkeWebkitUI*>(m_PaintManager.FindControl(_T("webkit")));

        //pWebkit = new CWkeWebkitUI();
        //pTabLayout_tab0->Add(pWebkit);

        /*wnd_tab1=new CMain1FrameWnd(pTabLayout_tab0);
        wnd_tab1->Create(GetHWND(),_T("wnd_tab1"),WS_CHILD | WS_VISIBLE, 0L);
        CDelegate<CMain1FrameWnd, CMain1FrameWnd> ltSize1(wnd_tab1, &CMain1FrameWnd::LayoutOnSize);
        pTabLayout_tab0->OnSize += ltSize1;*/
    }

    void CRuisMainFramWnd::InitWindow(){
        /*RECT rc;

        rc = pTabLayout_tab0->GetPos();
        rc.left += 4;
        rc.top += 2;
        rc.right -= 10;
        rc.bottom -= 10;
        pWebkit->SetPos(rc);
        pWebkit->SetURL(L"http://pcapp.1ydt.com");
        pWebkit->SetURL(L"http://v.youku.com/v_show/id_XMTM2Njg3Mjc5Mg==.html?f=20075970&from=y1.3-fun-fun-904-10077.88536-88537.1-1");*/
        TabOnSelected(0);
    }
    void CRuisMainFramWnd::Notify(TNotifyUI& msg)
    {
        CRuisBaseWindow::Notify(msg);
        if (msg.sType == DUI_MSGTYPE_CLICK) // 先判断消息类型，如果是单击
        {
            if (msg.pSender == btnClose) // 如果控件名为 btnClose
            {
                PostQuitMessage(0);
            }
            else if (msg.pSender == btnMax) {
                SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); return;
            }
            else if (msg.pSender == btnRestore) {
                SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); return;
            }
            else  if (msg.pSender == btnMin) {
                SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); return;
            }
        }
        else if (msg.sType == _T("selectchanged"))
        {
            CDuiString name = msg.pSender->GetName();
            if (name == _T("home")){
                vector<wstring> pars;
                pars.push_back(L"hello world");
                TabOnSelected(0);
                //ExecJsFun(L"alert", pars);
            }
            else if (name == _T("play"))
                TabOnSelected(1);
            else if (name == _T("shadu"))
                TabOnSelected(2);
        }
    }

    LRESULT CRuisMainFramWnd::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){
        BOOL bZoomed = ::IsZoomed(*this);
        if (::IsZoomed(*this) != bZoomed) {
            if (!bZoomed) {
                if (btnMax) btnMax->SetVisible(false);
                if (btnRestore) btnRestore->SetVisible(true);
            }
            else {
                if (btnMax) btnMax->SetVisible(true);
                if (btnRestore) btnRestore->SetVisible(false);
            }
        }
        return CRuisBaseWindow::OnSysCommand(uMsg, wParam, lParam,bHandled);
    }

    LRESULT CRuisMainFramWnd::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled) {
        if(wnd_tab1!=NULL) {
            wnd_tab1->ShowWindow(false);
            wnd_tab1->Close();
            delete wnd_tab1;
        }
        CRuisBaseWindow::OnDestroy(uMsg,wParam,lParam,bHandled);
    }

    void CRuisMainFramWnd::TabOnSelected(int ind){
        //pTabLayout->SelectItem(ind);
        /*wnd_tab1->ShowWindow(false);
        switch (ind)
        {
            case 0:
                wnd_tab1->ShowWindow(true);
                break;
        }*/
    }
}